<template>
  <div class="article-page">
    <nav class="my-nav van-hairline--bottom">
      <a
        @click="fd(null)"
        href="javascript:;"
        :class="[obj.sorter == null ? 'active' : '']"
        >推荐</a
      >
      <a
        @click="fd('weight_desc')"
        href="javascript:;"
        :class="[obj.sorter == 'weight_desc' ? 'active' : '']"
        >最新</a
      >
      <div class="logo"><img src="@/assets/logo.png" alt="" /></div>
    </nav>
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        offset="100"
        @load="onLoad"
      >
        <!-- 
组件身上没有标签元素提供的任何事件，
如果写了事件那就只能是子组件身上传过来的，通过$emit('click')
也可以通过native修饰符来触发它的原生事件
       -->
        <!-- <ArticleItem
          v-for="item in list"
          :key="item.id"
          :item="item"
          @click.native="fn(item.id)"
        ></ArticleItem> -->
        <ArticleItem
          v-for="(item, index) in list"
          :key="index"
          :item="item"
        ></ArticleItem>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { ArticleAPI } from '@/api/article.js'
export default {
  name: '',
  data () {
    return {
      list: [],
      loading: false,//加载数据，不加载了为true
      finished: false,//数据加载完毕为true,否则为false
      refreshing: false,//表示是否正在下拉刷新,刷新时为true,刷新后为false
      obj: {
        current: 0,
        pageSize: 10,
        sorter: null
      }
    }
  },
  created () {
    // this.getArticleList()
  },
  computed: {
  },
  methods: {

    fd (sorter) {
      this.obj.sorter = sorter
      this.refreshing = true
      this.onLoad()

    },
    async getArticleList () {
      try {
        const res = await ArticleAPI(this.obj)
        console.log(res, 22222222);
        // this.list = res.data.rows
        this.list = this.refreshing ? res.data.rows : [...this.list, ...res.data.rows]
        this.list = [...this.list, ...res.data.rows]
        this.loading = false//结束当前加载
        this.refreshing = false//关闭下拉
      } catch (error) {

      }

    },
    onLoad () {
      // this.loading = false//上提页面数据正在加载时该值为true
      // 数据全部加载完成
      if (this.obj.current >= 5) {
        this.finished = true//当finished为fals的时候页面数据底部无法再加载
        this.loading = false
        // this.refreshing = false
        return
      }
      this.obj.current += 1
      this.getArticleList()

    },
    onRefresh () {
      this.obj.current = 0// 重新加载数据
      this.loading = true
      this.onLoad()//再次调用
    },
  }
}
</script>
<style lang="less" scoped>
.article-page {
  margin-bottom: 50px;
  margin-top: 44px;
  .my-nav {
    height: 44px;
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    z-index: 999;
    background: #fff;
    display: flex;
    align-items: center;
    > a {
      color: #999;
      font-size: 14px;
      line-height: 44px;
      margin-left: 20px;
      position: relative;
      transition: all 0.3s;
      &::after {
        content: '';
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        bottom: 0;
        width: 0;
        height: 2px;
        background: #222;
        transition: all 0.3s;
      }
      &.active {
        color: #222;
        &::after {
          width: 14px;
        }
      }
    }
    .logo {
      flex: 1;
      display: flex;
      justify-content: flex-end;
      > img {
        width: 64px;
        height: 28px;
        display: block;
        margin-right: 10px;
      }
    }
  }
}
.article-item {
  .head {
    display: flex;
    img {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      overflow: hidden;
    }
    .con {
      flex: 1;
      overflow: hidden;
      padding-left: 10px;
      p {
        margin: 0;
        line-height: 1.5;
        &.title {
          width: 280px;
        }
        &.other {
          font-size: 10px;
          color: #999;
        }
      }
    }
  }
  .body {
    font-size: 14px;
    color: #666;
    line-height: 1.6;
    margin-top: 10px;
  }
  .foot {
    font-size: 12px;
    color: #999;
    margin-top: 10px;
  }
}
</style>